#!/bin/bash

usage(){
    echo
    echo "txpower_atheros - Script to set txpower for Atheros and Realtek cards"
    echo
    echo "Usage: $0 <txpower> (default 58, minimum 1, maximum 63)"
    echo
    echo "Example: txpower_atheros 50"
    echo
    exit 1
}

[[ $# -eq 0 ]] && usage

mv /etc/modprobe.d/ath9k_hw.conf /etc/modprobe.d/ath9k_hw.conf.old
sed -r -e's/txpower=[^ ]+/txpower='$1'/g' /etc/modprobe.d/ath9k_hw.conf.old > /etc/modprobe.d/ath9k_hw.conf
mv /etc/modprobe.d/rtl88XXau.conf /etc/modprobe.d/rtl88XXau.conf.old
sed -r -e's/rtw_tx_pwr_idx_override=[^ ]+/rtw_tx_pwr_idx_override='$1'/g' /etc/modprobe.d/rtl88XXau.conf.old > /etc/modprobe.d/rtl88XXau.conf
mv /etc/modprobe.d/rtl8812au.conf /etc/modprobe.d/rtl8812au.conf.old
sed -r -e's/rtw_tx_pwr_idx_override=[^ ]+/rtw_tx_pwr_idx_override='$1'/g' /etc/modprobe.d/rtl8812au.conf.old > /etc/modprobe.d/rtl8812au.conf
sync
echo
echo "txpower set to $1. You may power-off now or type \"reboot\" to restart"
echo
